package com.atguigu.tingshu.dispatch.job;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.dispatch.mapper.XxlJobLogMapper;
import com.atguigu.tingshu.model.dispatch.XxlJobLog;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * title:
 * author: zgl
 * date: 2024/3/13
 * description：
 */
@Component
public class UserJobHandler {
    @Resource
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private XxlJobLogMapper xxlJobLogMapper;
    @XxlJob("userUpdateVipState")
    public void updateVipState(){
        long start = System.currentTimeMillis();
        XxlJobLog xxlJobLog = new XxlJobLog();
        try {
            Result result = userInfoFeignClient.updateVipState();
            xxlJobLog.setJobId(XxlJobHelper.getJobId());
            Assert.notNull(result, "远程调用修改vip状态接口失败！");
            //成功
            xxlJobLog.setStatus(1);
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            xxlJobLog.setError(e.getMessage());
            xxlJobLog.setStatus(0);
        } finally {
            long end = System.currentTimeMillis();
            xxlJobLog.setTimes((int)(end-start));
            this.xxlJobLogMapper.insert(xxlJobLog);
        }
    }
}
